import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

public class Debug {

	private class PerfRecord {
		public long startTime;
		public long endTime;
	}

	public static final String DEBUG_FOOD = "Food";
	public static final String DEBUG_TURN = "Turn";
	public static final String DEBUG_REMAIN_TIME = "Remain_Time";
	public static final String DEBUG_ROUTE = "Route";
	public static final String DEBUG_TIME = "Time";
	public static final String DEBUG_EXPLORE_INIT = "Explore_Init";
	public static final String DEBUG_EXPLORE = "Explore";
	public static final String DEBUG_EXPLORE_PRIORITY = "Explore_Priority";
	public static final String DEBUG_EXPLORE_PRIORITY_DEBUG = "Explore_Priority_Debug";
	public static final String DEBUG_GOAL_DEBUG = "Goal_Debug";
	public static final String DEBUG_DIFF_DEBUG = "Diffusion_Debug";
	public static final String DEBUG_EXECUTE_PRE = "Execute_Prepare";
	public static final String DEBUG_EXECUTE = "Execute";
	public static final String DEBUG_PERFORMANCE = "Performance";
	public static final String DEBUG_DUMMY_MOVE = "Dummy_Move";
	public static final String DEBUG_ATTACK = "Attack";
	public static final String DEBUG_ATTACK_DEBUG = "Dummy_Attack_Debug";

	protected boolean enabled = false;
	protected FileWriter writer;
	protected static final ArrayList<String> LOG_TAGS = new ArrayList<String>();

	private HashMap<String, PerfRecord> performanceRecord = new HashMap<String, PerfRecord>();

	public Debug() {
//		LOG_TAGS.add(DEBUG_TURN);
		// LOG_TAGS.add(DEBUG_FOOD);
		// LOG_TAGS.add(DEBUG_REMAIN_TIME);
		// LOG_TAGS.add(DEBUG_ROUTE);
		// LOG_TAGS.add(DEBUG_TIME);
		// LOG_TAGS.add(DEBUG_EXPLORE);
		// LOG_TAGS.add(DEBUG_EXPLORE_PRIORITY);
		// LOG_TAGS.add(DEBUG_EXPLORE_PRIORITY_DEBUG);
		// LOG_TAGS.add(DEBUG_DIFF_DEBUG);
		// LOG_TAGS.add(DEBUG_ATTACK);
		// LOG_TAGS.add(DEBUG_ATTACK_DEBUG);
		// LOG_TAGS.add(DEBUG_GOAL_DEBUG);
		// LOG_TAGS.add(DEBUG_DUMMY_MOVE);
		// LOG_TAGS.add(DEBUG_EXPLORE_INIT);
		// LOG_TAGS.add(DEBUG_EXECUTE_PRE);
//		LOG_TAGS.add(DEBUG_EXECUTE);
//		LOG_TAGS.add(DEBUG_PERFORMANCE);

		if (enabled) {
			try {
				writer = new FileWriter("debug.log");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	protected boolean needLog(String tag) {
		return LOG_TAGS.contains(tag);
	}

	public void log(String tag, String message) {
		if (enabled && needLog(tag)) {
			try {
				writer.write(String.format("%s, %s\r\n", tag, message));
				writer.flush();
			} catch (IOException e) {
				throw new RuntimeException("Exception");
			}
		}
	}

	public void log(String tag, String message, String... args) {
		if (enabled && needLog(tag)) {
			try {
				writer.write(String.format("{0}, {1}\r\n", tag, message, args));
				writer.flush();
			} catch (IOException e) {
				throw new RuntimeException("Exception");
			}
		}
	}

	public void close() {

		if (enabled) {
			try {
				writer.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public void logPerformanceStart(String tag) {
		if (enabled) {
			PerfRecord rec = new PerfRecord();
			rec.startTime = System.currentTimeMillis();
			performanceRecord.put(tag, rec);
		}
	}

	public void logPerformanceEnd(String tag) {
		if (enabled && performanceRecord.containsKey(tag)) {
			PerfRecord rec = performanceRecord.get(tag);
			rec.endTime = System.currentTimeMillis();
			log(DEBUG_PERFORMANCE,
					String.format("%s - %d", tag, rec.endTime - rec.startTime));
			performanceRecord.remove(tag);
		}
	}
}
